package cz.gillan.housecosts;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import cz.gillan.housecosts.data.Gauge;

public class GaugeAdapter extends ArrayAdapter<Gauge> {
	private final static int resourceId = R.layout.gauge_item;
	private final Activity context;
	
	public GaugeAdapter(Activity context, List<Gauge> gauges) {
		super(context, resourceId, gauges);
		this.context = context;
	}
	
	static class ViewHolder {
		TextView nameTextView;
		TextView consumptionTextView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		View rowView = convertView;	
		
		if (rowView == null) {	
			
			LayoutInflater inflater = context.getLayoutInflater();		
			rowView = inflater.inflate(resourceId, null, true);		
					
			holder = new ViewHolder();		
					
			holder.nameTextView = (TextView) rowView.findViewById(R.id.gaugeItemNameText);
			holder.consumptionTextView = (TextView) rowView.findViewById(R.id.gaugeItemConsumptionText);		
			rowView.setTag(holder);		
		} else {		
			holder = (ViewHolder) rowView.getTag();		
		}		
		holder.nameTextView.setText(getItem(position).getName());
		String xx = String.format("%2.2f %s",  getItem(position).getConsumption(), getItem(position).getMagnitude());
		holder.consumptionTextView.setText(xx);		
		
		return rowView;
	}
	
	/*
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder holder = (ViewHolder)view.getTag();
		setHolderValues(holder, cursor);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		LayoutInflater inflater = ((Activity)context).getLayoutInflater();
		View view = inflater.inflate(resourceId, null, true);
		ViewHolder holder = new ViewHolder();
		holder.nameTextView = (TextView) view.findViewById(R.id.gaugeItemNameText);
		holder.consumptionTextView = (TextView) view.findViewById(R.id.gaugeItemConsumptionText);
		view.setTag(holder);
		setHolderValues(holder, cursor);
		return view;
	}
	
	private void setHolderValues(ViewHolder viewHolder, Cursor cursor) {
		viewHolder.nameTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(GaugeDbAdapter.KEY_NAME)));
		Double firstState = cursor.getDouble(cursor.getColumnIndexOrThrow(GaugeDbAdapter.KEY_FIRSTSTATE));
		Double lastState = cursor.getDouble(cursor.getColumnIndexOrThrow(GaugeDbAdapter.KEY_LASTSTATE));
		String magnitude = cursor.getString(cursor.getColumnIndexOrThrow(GaugeDbAdapter.KEY_MAGNITUDE));
		
		
		viewHolder.consumptionTextView.setText(xx);
	}
	*/
}
